home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / jpl_c.zip / HYPOT.C < prev    next >
Text File  |  1986-05-18  |  1KB  |  52 lines

  1. /* 1.0  07-06-84 */
  2. /************************************************************************
  3.  *            Robert C. Tausworthe                *
  4.  *            Jet Propulsion Laboratory            *
  5.  *            Pasadena, CA 91009        1984        *
  6.  ************************************************************************/
  7.  
  8. #include "defs.h"
  9. #include "stdtyp.h"
  10. #include "errno.h"
  11. #include "mathtyp.h"
  12. #include "mathcons.h"
  13.  
  14. /************************************************************************/
  15.     double
  16. hypot(x, y)        /* return hypotenuse value of x-y triangle    */
  17.  
  18. /*----------------------------------------------------------------------*/
  19. double x, y;
  20. {
  21.     double mx, t;
  22.  
  23.     if (x < 0.0)
  24.         x = -x;
  25.     if (y < 0.0)
  26.         y = -y;
  27.     mx = MAX(x, y);
  28.     if (x IS 0.00)
  29.         return y;
  30.  
  31.     if (y IS 0.0)
  32.         return x;
  33.  
  34.     if (mx < RTHLFINF)        /* we can use fast formula if true */
  35.         return sqrt(x*x + y*y);
  36.  
  37.     if (x > y)            /* otherwise, must use trig form.  */
  38.         t = atan2(y, x);
  39.     else
  40.         t = atan2(x, y);
  41.     t = cos(t);
  42.     if (mx < INFINLEG)
  43.         return (mx / t);    /* ok because t > 0.707          */
  44.  
  45.     t = log(mx) - log(t);        /* but now it's time for drastic  */
  46.     if (t < LOGINFINITY)        /* measures.              */
  47.         return exp(t);
  48.  
  49.     errno = ERANGE;            /* but all has failed.          */
  50.     return INFINITY;
  51. }
  52.